package market

import (
	"LogisticsPort/model"
	"LogisticsPort/sql"
	"github.com/gin-gonic/gin"
	"gorm.io/gorm"
)

func city(market *gin.RouterGroup) {
	cityRoute := market.Group("/city")
	{
		cityRoute.POST(sql.RequestAdd, cityAdd)
		cityRoute.POST(sql.RequestUpdate, cityUpdate)
		cityRoute.GET(sql.RequestData, cityData)
		cityRoute.GET(sql.RequestList, cityList)
		cityRoute.GET("/tree", cityTree)
		cityRoute.GET(sql.RequestDelete, cityDelete)
	}
}

func cityAdd(c *gin.Context) {
	sql.HttpAdd(c, &model.MarketCity{}, nil)
}

func cityUpdate(c *gin.Context) {
	sql.HttpUpdates(c, &model.MarketCity{}, nil)
}

func cityData(c *gin.Context) {
	sql.HttpDataById(c, &model.MarketCity{})
}

func cityDelete(c *gin.Context) {
	sql.HttpDeleteById(c, &model.MarketCity{})
}

func cityTree(c *gin.Context) {
	sql.HttpHandle(sql.GET, c, func(db *gorm.DB) (*gorm.DB, interface{}) {
		var marketList []model.Market
		var order = "CONVERT(CONCAT(province,city,area) USING gbk ) COLLATE gbk_chinese_ci"
		db.Preload("MarketCustomer").Order(order).Find(&marketList)

		//一级：省
		var provinceList []model.MarketCityTree
		for _, market := range marketList {
			marketNode := model.MarketNode{
				MarketId:       market.ID,
				Market:         market.Area + market.Market,
				CustomerNumber: len(market.MarketCustomer),
			}

			if !isContainerProvince(provinceList, market) { //不包含这个省，城市和市场新增进去，
				var province = model.MarketCityTree{Province: market.Province}
				cityInfo := model.CityNode{
					City:         market.City,
					Market:       []model.MarketNode{marketNode},
					MarketNumber: marketNode.CustomerNumber,
				}
				province.CityNumber = marketNode.CustomerNumber
				province.City = append(province.City, cityInfo)
				provinceList = append(provinceList, province)
			} else { //包含这个省，判断是否包含这个市
				for j, province := range provinceList {
					if province.Province == market.Province {
						if !isProvinceContainCity(province.City, market.City) {
							cityInfo := model.CityNode{
								City:         market.City,
								Market:       []model.MarketNode{marketNode},
								MarketNumber: marketNode.CustomerNumber,
							}
							provinceList[j].CityNumber = provinceList[j].CityNumber + marketNode.CustomerNumber
							provinceList[j].City = append(provinceList[j].City, cityInfo)
						} else {
							for i, iCity := range province.City {
								if iCity.City == market.City {
									iCity.Market = append(iCity.Market, marketNode)
									provinceList[j].City[i] = iCity
									provinceList[j].CityNumber = provinceList[j].CityNumber + marketNode.CustomerNumber
								}
							}
						}
					}
				}
			}
		}

		//根据条件查询子类
		/*
			db.Table("market").Preload("MarketCustomer", func(db *gorm.DB) *gorm.DB {
				db = db.Where("market_customer.area = area")
				return db
			}).Order("id desc").Find(&marketList)*/
		return db, marketList
	})
}

func cityList(c *gin.Context) {
	sql.HttpHandle(sql.GET, c, func(db *gorm.DB) (*gorm.DB, interface{}) {
		var marketList []model.Market
		var cityList []model.MarketCity
		var order = "CONVERT(CONCAT(province,city) USING gbk ) COLLATE gbk_chinese_ci"
		db.Order(order).Find(&marketList)

		for _, market := range marketList {
			var newMarketCity = model.MarketCity{Province: market.Province, City: market.City}
			if len(cityList) == 0 {
				cityList = append(cityList, newMarketCity)
			} else {
				if !isContainerCity(cityList, newMarketCity) {
					cityList = append(cityList, newMarketCity)
				}
			}
		}

		return db, cityList
	})
}

func isContainerCity(cityList []model.MarketCity, newMarketCity model.MarketCity) bool {
	for _, marketCity := range cityList {
		if marketCity.City == newMarketCity.City {
			return true
		}
	}
	return false
}

func isContainerProvince(provinceList []model.MarketCityTree, market model.Market) bool {
	for _, marketCity := range provinceList {
		if marketCity.Province == market.Province {
			return true
		}
	}
	return false
}

func isProvinceContainCity(cityList []model.CityNode, city string) bool {
	for _, cityName := range cityList {
		if cityName.City == city {
			return true
		}
	}
	return false
}
